package com.escool.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.TbTipoRecord;
import com.escool.service.TipoRecordService;

@Controller
public class TipoRecordController {
	
	@Autowired
	TipoRecordService tipoRecordService;
	
	@RequestMapping(value = "showTipoRec")
	public String showTipoRec(@ModelAttribute("tiporec") @Valid TbTipoRecord tbTipoRecord,BindingResult result, Map<String, Object> map){
		map.put("tiporec", new TbTipoRecord());
		map.put("tiporecList", tipoRecordService.listAll());
		return "/tipoRecord";
	}
	
	@RequestMapping(value = "addTipoRec", method = RequestMethod.POST)
	public String addTipoRec(@ModelAttribute("tiporec") @Valid TbTipoRecord tbTipoRecord,BindingResult result, Map<String, Object> map){
		if(!result.hasErrors()){
		tipoRecordService.add(tbTipoRecord);
		return "redirect:/showTipoRec";
		}
		return "";
	}
	
	@RequestMapping(value = "editTipoRec", method = RequestMethod.POST)
	public String editTipoRec(@ModelAttribute("tiporec") @Valid TbTipoRecord tbTipoRecord,BindingResult result, Map<String, Object> map){
		if(!result.hasErrors()){
		tipoRecordService.edit(tbTipoRecord);
		return "redirect:/showTipoRec";
		}
		return "";
	}
	
	@RequestMapping(value = "deleteTipoRec", method = RequestMethod.POST)
	public String deleteTipoRec(@ModelAttribute("tiporec") @Valid TbTipoRecord tbTipoRecord,BindingResult result, Map<String, Object> map){
		if(!result.hasErrors()){
		tipoRecordService.delete(tbTipoRecord.getIdTipoRecord());
		return "redirect:/showTipoRec";
		}
		return "";
	}

}
